<?php
class Mozg_Property_Config extends Mozg_Config {

	protected $_fields;
	protected $_default;

	public static function Get($path = false)
	{
		return self::GetInstance('data/'. $path, get_class());
	}

	public function GetValidatorClass()
	{
		return isset($this->_config['validator']) && $this->_config['validator'] ? $this->_config['validator'] : 'Mozg_Property_Validator';
	}

	public function GetEntityClass()
	{
		return isset($this->_config['entity']) && $this->_config['entity'] ? $this->_config['entity'] : 'Mozg_Property_Params';
	}

	public function GetFields()
	{
		if (is_array($this->_fields)) return $this->_fields;

		$this->_fields = $this->_config['fields'];
		if (is_array($this->_fields))
		{
			$this->_fields = array_keys($this->_fields);
		}
		else
		{
			$this->_fields = array();
		}

		return $this->_fields;
	}

	public function GetPrimaryKey()
	{
		return isset($this->_config['primaryKey']) ? $this->_config['primaryKey'] : false;
	}

	public function GetFieldType($fieldName)
	{
		return isset($this->_config['fields'][$fieldName]['type']) ? $this->_config['fields'][$fieldName]['type'] : null;
	}

	public function GetEntityPath($fieldName)
	{
		return isset($this->_config['fields'][$fieldName]['entityPath']) ? $this->_config['fields'][$fieldName]['entityPath'] : 'default';
	}

	public function IsMultilangField($fieldName)
	{
		return isset($this->_config['fields'][$fieldName]['multilang']) ? $this->_config['fields'][$fieldName]['multilang'] : null;
	}

	public function GetDefault()
	{
		if (!$this->_default)
		{
			$data = array();
			$fields = $this->GetFields();
			foreach ($fields as $fieldName)
			{
				$data[$fieldName] = isset($this->_config['fields'][$fieldName]['value']) ? $this->_config['fields'][$fieldName]['value'] : null;
			}

			$entityClass = $this->GetEntityClass();
			$this->_default = new $entityClass($this, $data);
		}

		return $this->_default;
	}

}
